package com.githan.bootsecurity.config;

import com.githan.bootsecurity.response.Result;
import com.githan.bootsecurity.response.ResultCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author https://github.com/Han56
 * @date 2021/4/11 19:00
 */
@Component
public class MyAuthenticationEntryPoint extends JSONAuthentication implements AuthenticationEntryPoint {

    /**
     *  匿名用户访问无权限资源时的异常
     */

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Result result=Result.error(ResultCode.USER_NOT_LOGIN);
        this.WriteJSON(httpServletRequest,httpServletResponse,result);
    }
}
